﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Devices;
using Devices.Healvet;

namespace DevicesWPF.UCDevices
{
    /// <summary>
    /// UCFF_6450.xaml 的交互逻辑
    /// </summary>
    public partial class UCHEALVET_HV_FIA3000 : UserControl, IUCConfig
    {
        HV_FIA3000Config config;
        HV_FIA3000 info;
        public UCHEALVET_HV_FIA3000()
        {
            InitializeComponent();
            radioButtonTCP.Checked += RadioButtonTCP_Checked;
            //radioButtonSerial.Checked += RadioButtonSerial_Checked;
        }
        public UCHEALVET_HV_FIA3000(HV_FIA3000 _info)
        {
            InitializeComponent();
            //radioButtonTCP.Checked += RadioButtonTCP_Checked;
            //radioButtonSerial.Checked += RadioButtonSerial_Checked;
            this.info = _info;
        }

        /// <summary>
        /// 读取配置
        /// </summary>
        /// <param name="hasErrors"></param>
        /// <returns></returns>
        public Config GetConfig(out int hasErrors)
        {
            hasErrors = 0;
            Config cf = info.Config;
            config = (HV_FIA3000Config)cf;
            config.CurrentConnectType = radioButtonTCP.IsChecked == true ? ConnectType.UDP : ConnectType.RS_232;
            config.IP = labelIP.Text;
            config.Port = int.Parse(textPort.Text);
            return cf;
        }

        private void NewUCNX500IVCConfig_Load(object sender, RoutedEventArgs e)
        {
            if (this.info != null)
            {
                config = (HV_FIA3000Config)info.Config;
                if (info.Config.CurrentConnectType == ConnectType.UDP)
                {
                    radioButtonTCP.IsChecked = true;
                    if (string.IsNullOrEmpty(config?.IP))
                    {
                        IPAddress ip = GetLocalIPAddress();
                        if (ip == null)
                        {
                            MessageBox.Show("请打开本机IPV4网络连接");
                            return;
                        }
                        config.IP = ip.ToString();
                    }
                    labelIP.Text = config.IP;
                    textPort.Text = config.Port.ToString();
                }
                //this.ucSerialPortConfig1.LoadData(config.SerialPortConfig);
            }
        }

        private void RadioButtonSerial_Checked(object sender, RoutedEventArgs e)
        {
            ucSerialPortConfig1.Visibility = Visibility.Visible;
            gridLocalIP.Visibility = Visibility.Hidden;
        }

        private IPAddress GetLocalIPAddress()
        {
            IPAddress[] ips = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            ips = ips?.Where(o => o.AddressFamily == AddressFamily.InterNetwork)?.ToArray();
            if (ips == null || ips.Length == 0)
                return null;
            return ips[0];
        }

        private void RadioButtonTCP_Checked(object sender, RoutedEventArgs e)
        {
            ucSerialPortConfig1.Visibility = Visibility.Hidden;
            gridLocalIP.Visibility = Visibility.Visible;
        }
    }
}
